package br.com.cogerh.sidec.dao;

import java.util.ArrayList;
import java.util.List;

import org.hibernate.Criteria;
import org.hibernate.HibernateException;
import org.hibernate.Session;
import org.hibernate.Transaction;

import br.com.cogerh.sidec.model.Gerente;
import br.com.cogerh.sidec.model.Pessoa;
import br.com.cogerh.sidec.util.HibernateUtil;

public class GerenteImpl implements GerenteDAO {



	@Override
	public void salvar(Gerente gerente) {
		Session sessao = HibernateUtil.getSessionFactory().getCurrentSession();
		Transaction tx = sessao.beginTransaction();
	try{
		sessao.save(gerente);
		sessao.flush();
		sessao.close();
		tx.commit();
	}catch(HibernateException e){
		tx.rollback();
	}
		
		}

	@SuppressWarnings("unchecked")
	@Override
	public List<Gerente> listar() {
		Session sessao = HibernateUtil.getSessionFactory().getCurrentSession();
		Transaction tx = sessao.beginTransaction();

		List<Gerente> lista = new ArrayList<Gerente>();
		try{
			Criteria criteria = sessao.createCriteria(Gerente.class);
			lista = criteria.list();
			sessao.close();
		}catch(HibernateException e){
			tx.rollback();
		}
		
		return lista;
	}

	@Override
	public void editar(Gerente gerente) {
		Session sessao = HibernateUtil.getSessionFactory().getCurrentSession();
		Transaction tx = sessao.beginTransaction();
	try{
		sessao.merge(gerente);
		sessao.flush();
		sessao.close();
		tx.commit();
	}catch(HibernateException e){
		tx.rollback();
	}
	
		
	}

	@Override
	public void excluir(Gerente gerente) {
		Session sessao = HibernateUtil.getSessionFactory().getCurrentSession();
		Transaction tx = sessao.beginTransaction();
		try{
			sessao.delete(gerente);
			sessao.flush();
			sessao.close();
			tx.commit();
		}catch(HibernateException e){
			tx.rollback();
		}
		
		}

	@Override
	public void fecharSessao() {
		
	}

}
